package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 汉网新闻解析
 * @author Persh
 *
 */
public class CnhanNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.cnhan.com/.*/[0-9]{4}-[0-9]{2}/[0-9]{2}/.*_[0-9]*.htm",
		"http://news.cnhan.com/html/.*/[0-9]{4}/[0-9]{4}/[0-9].*.html",
		"http://news.cnhan.com/html/.*/[0-9]*/[0-9]*.htm"
		};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("div#content.group>h1").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("td.td2>table>tr>td>h1").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("h1").text();
				if(StringUtils.isBlank(title)){
					title =doc.select("h1.post-title").text();
				}
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div#content.group"));
		if(StringUtils.isBlank(content)){
			if(doc.select("td.td2>table>tr").size()>7){
				content=HtmlCleaner.getContentHtml(url,doc.select("td.td2>table>tr").get(6));
			}else{
				content=HtmlCleaner.getContentHtml(url,doc.select("div#wz_bk"));
			}
		}
		if(StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url,doc.select("div#post-article"));
		}
		if(StringUtils.isBlank(content)){
			content =doc.select(".post-article").html();
		}
		date=DateUtils.matchDate(doc.select("div#content.group>h2").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("td.td2>table>tr>td").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("h2").text());
				if(date==null){
					date=DateUtils.matchDate(url);
				}
			}
		}

			news.setUrl(url);

			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);


			String author= JSoupUtils.matchAuthor(doc, "来源:");
			news.setAuthor(author);
			return news;
	}

	public static void main(String[] args) {
		CnhanNewsAnalyse analyse=new CnhanNewsAnalyse();
		String url="http://news.cnhan.com/html/yaowen/20151009/452428.htm";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
		System.out.println(meta.getHtml());
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
